package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.mapper.BookInfoMapper;
import com.qf.mapper.ClassInfoMapper;
import com.qf.pojo.BookInfo;
import com.qf.pojo.ClassInfo;
import com.qf.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BookInfoServiceImpl implements BookInfoService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;

    /**
     * 查询所有书籍
     * @return
     */
    @Override
    public List<BookInfo> findAll() {

        return bookInfoMapper.findAll();
    }
    /**
     * 通过id查询书籍信息
     * @param bookId
     * @return
     */
    @Override
    public BookInfo findByIdWithClass(Long bookId) {
        return bookInfoMapper.findByIdWithClass(bookId);
    }
    /**
     * 通过关键字查询
     * @param searchWord
     * @return
     */
    @Override
    public List<BookInfo> findByWord(String searchWord) {
        if (StringUtils.isEmpty(searchWord)) {
            searchWord="";
        }
        searchWord = "%" + searchWord.trim() + "%";
        return bookInfoMapper.findByWord(searchWord);
    }

    /**
     * 通过bookId删除数据
     * @param bookId
     */
    @Override
    public void deleteByBookId(Long bookId) {
        bookInfoMapper.deleteByBookId(bookId);
    }

    /**
     * 增加图书
     * @param bookInfo
     */
    @Override
    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.addBook(bookInfo);
    }
    /**
     * 通过id查询书籍
     * @param bookId
     * @return
     */
    @Override
    public Map<String, Object> findById(Long bookId) {
        BookInfo bookInfo = bookInfoMapper.findByIdWithClass(bookId);
        List<ClassInfo> classInfo = classInfoMapper.findAll();

        Map<String,Object> map = new HashMap<>();
        map.put("bookInfo",bookInfo);
        map.put("classInfo",classInfo);
        return map;
    }

    /**
     * 更新书籍信息
     * @param bookInfo
     */
    @Override
    public void updateBookInfo(BookInfo bookInfo) {
        bookInfoMapper.updateBookInfo(bookInfo);
    }
    /**
     * 根据id更新信息
     * @param bookId
     */
    @Override
    public void updateBookInfoByBookId(Long bookId) {
        bookInfoMapper.updateBookInfoByBookId(bookId);

    }
}
